﻿//�趨�˵�Expend��ʱ�򲻲���AutoPostBack
function SetExpendNoPostBack( MsTreeView)
{
	MsTreeView.onSelectedIndexChange=function()
	{		
		MsTreeView.queueEvent('onselectedindexchange',  event.oldTreeNodeIndex  +  ','  +  event.newTreeNodeIndex);  		
		window.setTimeout("__doPostBack(\'"+MsTreeView+"\',\'\')", 0,"JavaScript");
	}
}


//��ӦonCheck���¼���Ҫ��ִ�еĶ�����
function TreeOnCheck(tree,selectedCallBackMethod)
{
	node=tree.getTreeNode(tree.clickedNodeIndex);
	var IsChecked=tree.getTreeNode(tree.clickedNodeIndex).getAttribute("checked");
	var hiddenBox = document.getElementById(tree.id+"_checkBoxSelected")				
	if(IsChecked)
	{
		AddNodeId(hiddenBox,node,selectedCallBackMethod);
	}
	else
	{
		RemoveNodeId(hiddenBox,node,selectedCallBackMethod);
	}
	SetParent(node,IsChecked,hiddenBox,selectedCallBackMethod);
	SetCheck(node,IsChecked,hiddenBox,selectedCallBackMethod);	
	
}

function SetParent(node,Pc,hiddenBox,selectedCallBackMethod)
{	
	var parent=node.getParent();
	while(parent!=undefined && parent!=null)
	{
		parent.setAttribute("Checked",Pc);
		if(Pc)
		{
			AddNodeId(hiddenBox,parent,selectedCallBackMethod);
		}
		else
		{
			RemoveNodeId(hiddenBox,parent,selectedCallBackMethod);
		}
		parent=parent.getParent();
	}
}
//�����ӽڵ�ѡ��
function SetCheck(node,Pc,hiddenBox,selectedCallBackMethod)
{  
 var i;
 var ChildNode=new Array();
 ChildNode=node.getChildren(); 
 if(parseInt(ChildNode.length)==0)
 {
	return;
 }
 else
 {
	for(i=0;i<ChildNode.length;i++)
	{
		var cNode;
		cNode=ChildNode[i];
		if(parseInt(cNode.getChildren().length)!=0)
			SetCheck(cNode,Pc,hiddenBox,selectedCallBackMethod);
		cNode.setAttribute("Checked",Pc);		
		if(Pc)
		{
			AddNodeId(hiddenBox,cNode,selectedCallBackMethod);
		}
		else
		{
			RemoveNodeId(hiddenBox,cNode,selectedCallBackMethod);
		}
	}
  }
}
//���Ѿ�ѡ���Id�ü��뵽 hiddenText����
function AddNodeId(hiddenBox,Node,selectedCallBackMethod)
{		
	var condition=true;
	var idName=Node.getAttribute("id");	
	if(selectedCallBackMethod!=undefined && selectedCallBackMethod!=null)
	{
		//����лص�����������ִ�лص��������ٰѻص���������condition
				
		var returnValue=selectedCallBackMethod(Node);
		//��ֹ�ص�����û�з���ֵ��Ĭ��Ϊtrue;
		if(returnValue!=undefined)
		{
			condition=returnValue;			
		}
	}	
	else
	{
		var pos=hiddenBox.value.indexOf(","+idName+",")	
		condition=pos==-1
	}	
	if(condition)
	{
		hiddenBox.value+=","+idName;
	}   
}
//��idName�Ƴ�hiddenBox
function RemoveNodeId(hiddenBox,Node,selectedCallBackMethod)
{	
	var selectIds=hiddenBox.value;	
	var idName=Node.getAttribute("id");
	var condition=true;
	
	if(selectedCallBackMethod!=undefined && selectedCallBackMethod!=null)
	{
		var returnValue=selectedCallBackMethod(Node);
		if(returnValue!=undefined)
		{
			condition=returnValue;
		}
	}	
	else
	{
		var pos=hiddenBox.value.indexOf(","+idName+",")	
		condition=pos==-1
	}
		
	if(condition)
	{		
		str=selectIds.replace(","+idName,"");		
		hiddenBox.value=str;
	}	
}
//-------------------------------------------------------------�ط�����Ҫ���³�ʼ����ѡ����Ŀ--------------------------
function InitCheckNode(tree)
{	
	var hiddenBox = document.getElementById(tree.id+"_checkBoxSelected");
	if(hiddenBox!=null)
		ini(tree,hiddenBox.value);
}

function ini(node,selectedNodes)
{	
	var ChildNode=new Array();	
	ChildNode=node.getChildren();	
	for( var i=0;i<ChildNode.length;i++)
	{
		var cNode;
		cNode=ChildNode[i];
		if(parseInt(cNode.getChildren().length)!=0)
			ini(cNode,selectedNodes);			
		
		var id=cNode.getAttribute("id")				
		var pos = selectedNodes.indexOf(","+id)
		if(pos!=-1)
		{			
			cNode.setAttribute("Checked","true")
		}		
	}	
}
